package socketing;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class V2Client {
    public static void main(String[] args) throws Exception {

        Scanner scanner = new Scanner(System.in);

        Socket socket = new Socket("master", 4444);
        socket.setSoTimeout(8000);
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(
                        socket.getOutputStream(), StandardCharsets.UTF_8));
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        socket.getInputStream(), StandardCharsets.UTF_8));

        new Thread(()->{
            final BufferedReader readerl= reader;
            while (true){
                try {
                    String s = readerl.readLine();
                    if (s==null)break;
                    Tool.debug("res: "+s);
                } catch (IOException e) {
                    Tool.debug("timeout");
                }

            }
        },"回复回调专用线路").start();
        while (true){
            String s = scanner.nextLine();
            if (s.equals("quit"))break;
            writer.write(s);
            writer.newLine();
            writer.flush();
        }
        reader.close();
        writer.close();
        socket.close();

    }
}
